//
//  THFeedBackView.m
//  TaiHao
//
//  Created by rzm on 16/5/31.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxFeedBackView.h"

@interface SampuxFeedBackView ()<UITextFieldDelegate,UITextViewDelegate>
@property (nonatomic, strong) UILabel *lab;

@end
@implementation SampuxFeedBackView

- (instancetype)initWithFrame:(CGRect)frame

{
    if (self = [super initWithFrame:frame]) {
        [self loadUI];
    }
    return self;
}

- (void)loadUI
{
    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 16;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 16;
    }else if (IS_IPHONE_6)
    {
        font = 18;
    }else
    {
        font = 20;
    }
    UIView *backView = [[UIView alloc]init];
    backView.frame = CGRectMake(12, 20, self.width - 12 * 2, self.height * 0.4);
    backView.backgroundColor = THWhiteAlplaColor(0.06);

    [self addSubview:backView];
    [backView setCorner:6];

    UIView *themeBackView = [[UIView alloc] initWithFrame:CGRectMake(11, SampuxMargin, backView.width - 11*2, Sampux6HScale(37))];
    themeBackView.userInteractionEnabled  = YES;
    [backView addSubview:themeBackView];
    [themeBackView setCorner:4];
//    themeBackView.backgroundColor =  THWhiteAlplaColor(0.8);
    themeBackView.backgroundColor = THWhiteAlplaColor(0.12);

    UIButton *themeImage = [[UIButton alloc]initWithFrame:CGRectMake(5, 5, Sampux6WScale(30), Sampux6HScale(25))];
    [themeBackView addSubview:themeImage];
    [themeImage  setImage:imageNamed(@"theme") forState:UIControlStateNormal];
    
    UIView *themelineView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(themeImage.frame)+ 2, themeBackView.height * 0.25, 1, themeBackView.height*0.5)];
    [themeBackView addSubview:themelineView];
    themelineView.backgroundColor = THWhiteAlplaColor(0.6);
    
    UITextField *themeF = [[UITextField alloc]initWithFrame:CGRectMake(themelineView.right + 10, themeBackView.height * 0.3, themeBackView.width - themelineView.right - 5,themeImage.height*0.6)];
    themeF.placeholder = @"添加主题";
    themeF.font = THFont(font - 4);
    [themeF setValue:THWhiteAlplaColor(0.6) forKeyPath:@"_placeholderLabel.textColor"];
    [themeBackView addSubview:themeF];
    themeF.delegate = self;
    
    UIView *feedBackView = [[UIView alloc] initWithFrame:CGRectMake(11, themeBackView.bottom + SampuxMargin, backView.width - 11*2, backView.height *0.5)];
    feedBackView.userInteractionEnabled  = YES;
    [backView addSubview:feedBackView];
    [feedBackView setCorner:4];
    feedBackView.backgroundColor =  THWhiteAlplaColor(0.12);

    UIButton *contentImage = [[UIButton alloc]initWithFrame:CGRectMake(5, 5, Sampux6WScale(30), Sampux6HScale(25))];
    [feedBackView addSubview:contentImage];
    contentImage.contentMode = UIViewContentModeCenter;
    [contentImage  setImage:imageNamed(@"feedBackCode") forState:UIControlStateNormal];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(contentImage.frame)+ 2, contentImage.height * 0.1 + contentImage.y , 1, themelineView.height)];
    [feedBackView addSubview:lineView];
    lineView.backgroundColor = THWhiteAlplaColor(0.6);
    
    
    UITextView *contentView = [[UITextView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lineView.frame) + 5 , 0, feedBackView.width - CGRectGetMaxX(lineView.frame) - 20, feedBackView.height)];
    contentView.delegate = self;
    contentView.font = [UIFont boldSystemFontOfSize:font - 4];
    contentView.contentInset = UIEdgeInsetsMake(0, 0, 5, 0);
    contentView.returnKeyType = UIReturnKeyDone;
    [feedBackView addSubview:contentView];
    contentView.backgroundColor = THSystemClearColor;
    contentView.textColor = THWhiteAlplaColor(0.6);
    contentView.delegate = self;
    
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake( 5, Sampux6HScale(10), 80, themeF.height)];
    lab.font = [UIFont systemFontOfSize:font];
    lab.textColor = THWhiteAlplaColor(0.6);
    lab.text = @"您的反馈";
    self.lab = lab;
    lab.font = THFont(font - 4);
    [contentView addSubview:lab];
    
    
    UIButton *btn = [UIButton createButtonWithTitle:@"保存" frame:CGRectMake(backView.width/2 - Sampux6WScale(345/2)/2, CGRectGetMaxY(feedBackView.frame) + Sampux6HScale(30),  Sampux6WScale(345/2), Sampux6HScale(40)) titleFont:THFont(font) titleColor:THDeFaultBlueColor];
    btn.layer.borderWidth = 1;
    btn.layer.borderColor = THWhiteAlplaColor(0.8).CGColor;
    [backView addSubview:btn];
    [btn setCorner:btn.height/2];
    [btn addTarget:self action:@selector(commitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    backView.height = btn.bottom + Sampux6HScale(25);
}

- (void)commitBtnClick
{
    if (self.popBlock) {
        self.popBlock();
    }
}
#pragma mark textViewDelegate
- (void)textViewDidChange:(UITextView *)textView
{
    //隐藏或显示lab
    if (textView.text == nil || textView.text.length == 0) {
        self.lab.hidden = NO;
    }else{
        self.lab.hidden = YES;
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}
/**
 *  收键盘
 */
- (void)textViewDidEndEditing:(UITextView *)textView
{
    [textView resignFirstResponder];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}

#pragma -mark 输入框代理
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [self layoutIfNeeded];
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight );
    return YES;
}
@end
